package cd.zhixuan.utils;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.NonNull;

/**
 * @author HuZhiChao
 * Create Time: 2022.01.04
 **/
public class SystemTools {
    /**
     * @param timeDelay 延迟重启时间
     */
    public static boolean prepareRestartApp(@NonNull Application app, long timeDelay) {
        try {
            Intent intent = app.getPackageManager().getLaunchIntentForPackage(app.getPackageName());
            PendingIntent restartIntent = PendingIntent.getActivity(app, 0, intent, 0);
            AlarmManager mgr = (AlarmManager) app.getSystemService(Context.ALARM_SERVICE);

            mgr.set(AlarmManager.RTC, timeDelay, restartIntent);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 显示/隐藏键盘
     * @param view
     * @param show
     */
    public static void showKeyboard(View view, boolean show) {
        InputMethodManager manager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (show) {
            manager.showSoftInput(view, InputMethodManager.SHOW_FORCED);
        } else {
            manager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
